<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Request;
use App\Models\GameConfigData;

// 玩家 根据游戏角色  新增 流失用户统计 只与昨日对比
class PlayerDataCommand extends  Model{

	protected $table='command_player_data';
    //protected $_db_select = 'mysql';
    const DB_FIELDS = 'id,command,desc';
    public $timestamps = true;

    public static function assignByOut(&$one, $out){
        $flds = explode(',', self::DB_FIELDS);
        
        foreach ($flds as $fld) {
            isset($out[$fld]) && $one->{$fld} = $out[$fld];
        }

    }

    protected static function send($one, $area = 0, $dataId = null, $dataCnt = null, $msgid = \App\Service\MessageDef::http_plat_game_request){

        \App\Models\SendSocketToGame::SendAction(function($socket)use($one, $playerId, $area){
            $data['msgid'] = $msgid ?:  \App\Service\MessageDef::http_plat_game_request;
            $data["area"] = $area ? intval($area) : \App\Models\Area::getPhysicsArea($one['area']); 
            $data["area"] = \App\Models\Area::getPhysicsArea($data['area']);
            $data['game_msgid'] = \App\Service\MessageDef::gm_add_player_data_req;
            $data["playerId"] = $one['player_id'];
            $data["game_msg"] = [  
                'playerId' => $one['player_id'],
                'command' => $one['command'],
                'account' => $one['account'],
                'dataId' => $dataId ?: $one['dataId'],
                'dataCnt' => $dataCnt ?: $one['dataCnt'],
            ];
            $ret = $socket->send($data);
            \Log::debug('send game command', [$data, $ret]);
        });
    }

    public static function listAll(){
        $state = $online ? 1 :0;

        $data = \DB::table('command_player_data')->where("deleted_at", 0)->get();
 
        //$data = $data->toArray();
        $rtn = [];
        foreach($data as $attr => $obj){
            $rtn[$obj->command] = $obj->desc;
        }
        return $rtn;
    }

    protected static function sendReloadConfig($ids, $area = 0, $msgid = \App\Service\MessageDef::plat_2_center_http_req){

        $data_list = GameConfigData::$data_list;

        $sync_list = [];
        foreach ($data_list as $k => $obj) {
            if (in_array($obj['id'], $ids) ) {
                $sync_list[] = $obj["file_name"];
            }
        }

        \App\Models\SendSocketToGame::SendAction(function($socket)use($sync_list, $playerId, $area){
            $data['msgid'] = $msgid ?:  \App\Service\MessageDef::plat_2_center_http_req;
            $data["area"] =  intval($area); 
            $data["area"] = \App\Models\Area::getPhysicsArea($data['area']);
            $data['game_msgid'] = \App\Service\MessageDef::plat_2_center_http_req;
            $data["cp_operator"] = 'reload_config';
            $data["body"] = [  
                'files' => $sync_list,
            ];
            $ret = $socket->send($data);
            \Log::debug('send game command:reload config to center', [$data, $ret]);
        });
    }

    // 开启gm工具 5分钟
    public static function sendOpenGmCommand($area, $msgid = \App\Service\MessageDef::http_plat_open_gm_command_request){
        \App\Models\SendSocketToGame::SendAction(function($socket)use($area){
            $data['msgid'] = $msgid ?:  \App\Service\MessageDef::http_plat_game_request;
            $data["area"] =  \App\Models\Area::getPhysicsArea($area); 
            $data["area"] = \App\Models\Area::getPhysicsArea($data['area']);
            $data['game_msgid'] = \App\Service\MessageDef::http_plat_open_gm_command_request;
            $data["body"] = [  
                'time_dur' => 0,
            ];
            $ret = $socket->send($data);
            \Log::debug('send game command:open gm command', [$data, $ret]);
        });


    }
}
